package com.github.eclipseace.mybatis.util;

/**
 * @author eclipseAce
 */
public final class StringUtils {
	private StringUtils() {
	}

	public static String convertCamelCaseToUnderscore(String camelCase) {
		if (camelCase == null || camelCase.isEmpty()) {
			return camelCase;
		}
		StringBuilder underscore = new StringBuilder();
		boolean leading = true;
		for (char ch : camelCase.toCharArray()) {
			if (Character.isUpperCase(ch)) {
				if (!leading) {
					underscore.append('_');
				}
				underscore.append(Character.toLowerCase(ch));
			} else {
				underscore.append(ch);
			}
			if (leading) {
				leading = false;
			}
		}
		return underscore.toString();
	}

	public static String join(String[] textArray, String separator) {
		if (textArray == null) {
			return null;
		}
		int length = textArray.length;
		if (length == 0) {
			return "";
		}
		if (length == 1) {
			return textArray[0];
		}
		StringBuilder builder = new StringBuilder(emptyIfNull(textArray[0]));
		for (int i = 1; i < length; ++i) {
			builder.append(separator).append(emptyIfNull(textArray[i]));
		}
		return builder.toString();
	}

	public static String joinWithComma(String[] textArray) {
		return join(textArray, ",");
	}

	public static boolean isEmpty(String text) {
		return text == null || text.isEmpty();
	}

	public static boolean isNotEmpty(String text) {
		return !isEmpty(text);
	}

	public static String emptyIfNull(String text) {
		return text != null ? text : "";
	}

	public static String defaultIfEmpty(String text, String emptyText) {
		return isNotEmpty(text) ? text : emptyText;
	}
}
